package com.example.wangzh.developkit.control;

import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.adapter.RcvTestAdapter;
import com.example.wangzh.developkit.base.BaseActivityNew;
import com.example.wangzh.developkit.databinding.ActivityRcvTestBinding;

/**
 * @author: zhwang37
 * @date: 2021/9/13 9:44
 * @email: zhwang37@iflytek.com
 * @describe: recyclerView平滑滚动到任意position(包括之前不可见的item, 限定在短时间内滚动完成 ， 该item可以定在屏幕任意位置 ）
 */
public class RcvTestActivity extends BaseActivityNew<ActivityRcvTestBinding> {
    RcvTestAdapter rcvTestAdapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_rcv_test;
    }

    @Override
    protected void initView() {
        rcvTestAdapter = new RcvTestAdapter();
        mBinding.rcv.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rcv.setAdapter(rcvTestAdapter);
        mBinding.rcv.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL));
        mBinding.btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoPosition(2979);
            }
        });
    }

    @SuppressWarnings("SameParameterValue")
    private void gotoPosition(int position) {
        View targetItem = findItemByRealPosition(mBinding.rcv, position);
        if (targetItem != null) {
            slightAdjust(targetItem, false);
        } else {
            int count = mBinding.rcv.getChildCount();
            if (count == 0) {
                return;
            }
            int first = (int) mBinding.rcv.getChildAt(0).getTag();
            int last = (int) mBinding.rcv.getChildAt(count - 1).getTag();
            if (position < first) {
                mBinding.rcv.smoothScrollBy(0, 100, new AccelerateInterpolator());
            } else if (position > last) {
                mBinding.rcv.smoothScrollBy(0, -100, new AccelerateInterpolator());
            }
            mBinding.rcv.scrollToPosition(position);
            mBinding.rcv.post(new Runnable() {
                @Override
                public void run() {
                    View targetItem = findItemByRealPosition(mBinding.rcv, position);
                    slightAdjust(targetItem, true);
                }
            });
        }
    }

    private void slightAdjust(View targetItem, boolean hasPreAnim) {
        int[] location = new int[2];
        targetItem.getLocationOnScreen(location);
        int dy = mBinding.rcv.getMeasuredHeight() / 4 - location[1];
        if (hasPreAnim) {
            mBinding.rcv.smoothScrollBy(0, -dy, new DecelerateInterpolator());
        } else {
            mBinding.rcv.smoothScrollBy(0, -dy);
        }
    }

    private View findItemByRealPosition(RecyclerView rcv, int position) {
        View target = null;
        int count = rcv.getChildCount();
        for (int i = 0; i < count; i++) {
            int realPosition = (int) rcv.getChildAt(i).getTag();
            if (realPosition == position) {
                target = rcv.getChildAt(i);
                break;
            }
        }
        return target;
    }
}
